#! /usr/bin/env python
# -*- coding: utf-8 -*-

# *************************************************************
#     Filename @  convert_txt_to_mdtable.py
#       Author @  Huoty
#  Create date @  2016-07-07 11:51:40
#  Description @  
# *************************************************************

import os
import sys
import argparse

def convert2mdtable(src, dest=None):
    with open(src) as in_fo:
        out_fo = open(dest, "w") if dest else sys.stdout
        for line in in_fo:
            line = line.strip()

            if "（" in line:
                line = line.replace("（", "|")

            if "）" in line:
                line = line.replace("）", "|")
            else:
                line = line + "||"

            line = line.replace(':', '|')
            line = "|" + line + "\n"
            out_fo.write(line)
        
        out_fo.close()
    pass



# Script starts from here

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("file", type=str)
    parser.add_argument("-o", "--out", type=str, default=None)

    options = parser.parse_args()

    if options.file:
        src = os.path.abspath(options.file)
        dest = os.path.abspath(options.out) if options.out else None
        convert2mdtable(src, dest)
    else:
        parser.print_help()
